package cn.nfwx.tv.manager;

import android.content.Context;

import com.chan.net.DefaultObserver;
import com.chan.net.HttpCallback;
import com.chan.net.Result;
import com.trello.rxlifecycle2.LifecycleTransformer;

import java.util.List;

import cn.nfwx.tv.callback.ResponseWithFailListener;
import cn.nfwx.tv.conts.Constants;
import cn.nfwx.tv.helper.AccountHelper;
import cn.nfwx.tv.helper.LocationHelper;
import cn.nfwx.tv.model.NyqfBean;
import cn.nfwx.tv.model.TvChannel;
import cn.nfwx.tv.okhttp.CustomOkHttpClient;
import cn.nfwx.tv.okhttp.Http;
import cn.nfwx.tv.okhttp.UrlHelper;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Callback;
import okio.ByteString;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;

/**
 * TV频道列表
 * Created by zb.liu on 2017/11/14.
 */

public class TVChannelManager {
    /**
     * 台标
     *
     * @param path
     */
    public static String getChannelImg(String path) {
        StringBuilder stringBuilder = new StringBuilder(Constants.base_video_url);
        stringBuilder.append(path);
        stringBuilder.append(".jpg?type=live&thumbnail=thumbnail_small.jpg");
        return stringBuilder.toString();
    }

    /**
     * 获取
     *
     * @return
     */
    public static String getChannelLives(Context context, LifecycleTransformer<Result<List<TvChannel>>> lifecycleTransformer, HttpCallback httpCallback) {
        String request_tag = "getChannelLives" + System.currentTimeMillis();
        request_tag = ByteString.encodeUtf8(request_tag).md5().hex();
        Api api = Http.getInstance().getLiveRetrofit(Api.class);
        Observable<Result<List<TvChannel>>> observable = api.getChannelLives(LocationHelper.getInstance().getRegion(),AccountHelper.getInstance().getUid(context));
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(lifecycleTransformer)
                .subscribe(new DefaultObserver(request_tag, httpCallback));
        return request_tag;
    }

    /**
     * 获取回看列表
     *
     * @param context
     * @param gid
     * @param day
     * @param callBack
     * @return
     */
    public static String getReviewVods(Context context, String gid, String day, Callback callBack) {
        String request_tag = "getLiveVods";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getReviewUrl(gid, day);
        CustomOkHttpClient.doGet(url, request_tag, callBack);
        return request_tag;
    }

    /**
     * 获取企业视频
     *
     * @param context
     * @param mobile
     * @param listener
     * @return
     */
    public static String getCompanyVods(Context context, String mobile, ResponseWithFailListener listener) {
        String request_tag = "getCompanyVods";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getCompanyVideoUrl(mobile);
        CustomOkHttpClient.getObjectRequest(context, url, request_tag, listener);
        return request_tag;
    }

    private interface Api {
        @GET("api/querylive")
        Observable<Result<List<TvChannel>>> getChannelLives(@Query("region") String region, @Query("uid") int uid);
    }
}
